package mercede.allManagerBeans;

import javax.ejb.Stateless;
import mercede.allManagers.FlightManager;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import mercede.allTable.Flight;
import mercede.allDTOs.FlightDTO;
import java.util.ArrayList;
import java.util.List;

/**
 * Session Bean implementation class FlightManagerBean
 */
@Stateless
public class FlightManagerBean implements FlightManager {
	
	   @PersistenceContext
	   private EntityManager em;
		
		@Resource
		private EJBContext context;
		

	    @Override
	    public void save(FlightDTO Flightdto) {
	            Flight newFlight= new Flight(Flightdto);
	            em.persist(newFlight);
	    }
	    
	       
	    @Override
	    public void remove(int fid) {
	 		Flight flight = find(fid);
	         em.remove(flight);
	 	}
	     
	     public void remove(Flight flight) {
	     	em.remove(flight);
	 	}
	     
	   
	   private Flight find(int fid) {
	   return em.find(Flight.class, fid);
	   }
	    
	   @Override
	   public ArrayList<?> getFlight() {
	   return (ArrayList<?>) em.createNamedQuery("Flight.findAll", Flight.class).getResultList();
	   }
	   
	   
	   @Override
	   public FlightDTO getFlightDTO(int fid) {
		   Flight newFlight =find(fid);
		   FlightDTO Flightdto = convertToDTO(newFlight);
	       return Flightdto;
	   }
	   

	   @Override
	   public List<FlightDTO> getListFlightDTO() {
	       List<Flight> newFlight= em.createNamedQuery("Flight.findAll", Flight.class).getResultList();
	       List<FlightDTO> flightdto = new ArrayList<>();
	       for(Flight f: newFlight) {
	    	   flightdto.add(convertToDTO(f));
	       }
	       return flightdto;
	   }
	   
	   

	   @Override
	   public List<FlightDTO> getListFlightDTOByDest(String fdestination) {
		   
	      Query query=em.createNamedQuery("Flight.FIND_BY_DEST", Flight.class);
	      query.setParameter("fdestination",fdestination);
		   List<Flight> newHotle= query.getResultList();
	       List<FlightDTO> flightdto = new ArrayList<>();
	       for(Flight f: newHotle) {
	    	   flightdto.add(convertToDTO(f));
	       }
	       return flightdto;
	   }
	   
	   public static List<FlightDTO> convertToDTOList(List<Flight> flightList) {
	       List<FlightDTO> flightdtoList = new ArrayList<FlightDTO>();
	       for(Flight f: flightList){
	    	   flightdtoList.add(convertToDTO(f));
	       }
	       return  flightdtoList;
	}   
	   
	   
	    public String getPrincipalName() {
	        return context.getCallerPrincipal().getName();
	    }
	    
	    public static  FlightDTO convertToDTO(Flight flight)
	    {
	    	FlightDTO newFlightdto=new FlightDTO();
	    	newFlightdto.setfID(flight.getFID());
	    	newFlightdto.setfDeparture(flight.getFDeparture());
	    	newFlightdto.setfDestination(flight.getFDestination());
	    	newFlightdto.setfNumber(flight.getFNumber());
	    	newFlightdto.setfPrice(flight.getFPrice());

	    	
	    	return newFlightdto;
	    	
	    }

    }


